//
//  OrderTableViewCell.m
//  cityStar
//
//  Created by Hancong on 14/12/30.
//  Copyright (c) 2014年 tiny. All rights reserved.
//

#import "OrderTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "RIButtonItem.h"
#import "UIActionSheet+Blocks.h"
#import "HttpRequestManager.h"
#import "KInterFace.h"
#import "SVProgressHUD.h"
#import "UIAlertView+Blocks.h"

@implementation OrderTableViewCell

+(CGFloat)heightOfTableCell
{
    return 116;
}

-(void)configWithGood:(OrderListGood *)orderGood
{
    self.commodityOrder = orderGood;
    [self.imageAvatar sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://api.icszx.com/image/c/id/%@/w/146/h/120/m/0",orderGood.image]] placeholderImage:[UIImage imageNamed:@"商品列表_img"]];
    self.labelContent.text = orderGood.goods_name;
    self.labelNowMoney.text = [NSString stringWithFormat:@"￥%@",orderGood.now_price];
//    self.labelOldMoney.text = [NSString stringWithFormat:@"￥%@",orderGood.market_price];
    self.labelNumber.text = [NSString stringWithFormat:@"x%@",orderGood.number];
    NSString *oldPrice = [NSString stringWithFormat:@"￥%@",orderGood.market_price];
    NSUInteger length = [oldPrice length];
    
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:oldPrice];
    [attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, length)];
    [attri addAttribute:NSStrikethroughColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, length)];
    [self.labelOldMoney setAttributedText:attri];
    if ([orderGood.goods_status intValue] == 2) {
        self.btnEvaluation.hidden = NO;
        self.btnApplyReturn.hidden = NO;
        self.btnWriteExpress.hidden = YES;
        self.labelReturnSuccess.hidden = YES;
    }else if ([orderGood.goods_status intValue] == 3){
        self.btnEvaluation.hidden = YES;
        self.btnApplyReturn.hidden = YES;
        self.btnWriteExpress.hidden = YES;
        self.labelReturnSuccess.hidden = NO;
        self.labelReturnSuccess.text = @"申请退货换货中...";
        self.labelReturnSuccess.textColor = [UIColor colorWithRed:0.404 green:0.675 blue:0.996 alpha:1.000];
    }else if ([orderGood.goods_status intValue] == 4){
        self.btnEvaluation.hidden = YES;
        self.btnApplyReturn.hidden = YES;
        self.btnWriteExpress.hidden = NO;
        self.labelReturnSuccess.hidden = NO;
        self.labelReturnSuccess.text = @"退货换货成功...";
        self.labelReturnSuccess.textColor = [UIColor colorWithRed:0.973 green:0.573 blue:0.369 alpha:1.000];
    }else if ([orderGood.goods_status intValue] == 4){
        self.btnEvaluation.hidden = YES;
        self.btnApplyReturn.hidden = YES;
        self.btnWriteExpress.hidden = YES;
        self.labelReturnSuccess.hidden = NO;
        self.labelReturnSuccess.text = @"退货换货失败...";
        self.labelReturnSuccess.textColor = [UIColor colorWithWhite:0.776 alpha:1.000];
    }

}
- (IBAction)applyReturn:(id)sender {
    RIButtonItem *ruturnButton = [RIButtonItem itemWithLabel:@"退货" action:^{
        NSDictionary *parameters = @{@"id":self.commodityOrder.objectId};
        [HttpRequestManager putMethod:KInterface_CommodityRejecte parameters:parameters completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            if (success) {
                [SVProgressHUD showSuccessWithStatus:@"申请成功"];
            }else{
                if ([response[@"code"] intValue] == 23003) {
                    [SVProgressHUD showErrorWithStatus:@"该订单不能退换货"];
                }else if ([response[@"code"] intValue] == 23004) {
                    [SVProgressHUD showErrorWithStatus:@"退换货申请提交失败"];
                }
            }
        }];
    }];
    RIButtonItem *changeButton = [RIButtonItem itemWithLabel:@"换货" action:^{
        NSDictionary *parameters = @{@"id":self.commodityOrder.objectId};
        [HttpRequestManager putMethod:KInterface_CommodityRejecte parameters:parameters completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            if (success) {
                [SVProgressHUD showSuccessWithStatus:@"申请成功"];
            }else{
                if ([response[@"code"] intValue] == 23003) {
                    [SVProgressHUD showErrorWithStatus:@"该订单不能退换货"];
                }else if ([response[@"code"] intValue] == 23004) {
                    [SVProgressHUD showErrorWithStatus:@"退换货申请提交失败"];
                }
            }
        }];
    }];
    UIActionSheet *actionsheet = [[UIActionSheet alloc]initWithTitle:nil cancelButtonItem:ruturnButton destructiveButtonItem:changeButton otherButtonItems: nil];
    [actionsheet showInView:self];
    
}
- (IBAction)writeExpress:(id)sender {
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"填写快递信息" message:nil delegate:self cancelButtonTitle:@"确认" otherButtonTitles:@"取消", nil];
    alertView.tag = 0;
    alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
    [alertView textFieldAtIndex:0].placeholder = @"输入快递单号";
    [[alertView textFieldAtIndex:1] setPlaceholder:@"输入快递公司名称"];

    [alertView show];
}
- (IBAction)evaluation:(id)sender {
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"评价" message:nil delegate:self cancelButtonTitle:@"确认" otherButtonTitles:@"取消", nil];
    alertView.tag = 1;
    alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alertView textFieldAtIndex:0].placeholder = @"输入评价信息";
    [alertView show];
}

-(void)alertView : (UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 0) {
        if (buttonIndex == 0) {
            //得到输入框
            UITextField *fieldNum = [alertView textFieldAtIndex:0];
            UITextField *fieldName = [alertView textFieldAtIndex:1];
            NSDictionary *parameters = @{@"id":self.commodityOrder.objectId,
                                         @"code":fieldNum.text,
                                         @"company":fieldName.text
                                         };
            [HttpRequestManager putMethod:KInterface_CommodityLogistics parameters:parameters completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
                if (success) {
                    [SVProgressHUD showSuccessWithStatus:@"提交物流信息成功"];
                }else{
                    if ([response[@"code"] intValue] == 23005) {
                        [SVProgressHUD showErrorWithStatus:@"该订单不能退换货"];
                    }else{
                    [SVProgressHUD showErrorWithStatus:errMsg];
                    }
                }
            }];
        }
    }else{
        if (buttonIndex == 0) {
            //得到输入框
            UITextField *fieldcontent = [alertView textFieldAtIndex:0];
//            UITextField *fieldName = [alertView textFieldAtIndex:1];
            NSDictionary *parameters = @{@"id":self.commodityOrder.objectId,
                                         @"content":fieldcontent.text
                                         };
            [HttpRequestManager postMethod:KInterface_CommodityComment parameters:parameters completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
                if (success) {
                    [SVProgressHUD showSuccessWithStatus:@"提交商品评价成功"];
                }else{
                    if ([response[@"code"] intValue] == 23001) {
                        [SVProgressHUD showErrorWithStatus:@"请先登录"];
                    }else if ([response[@"code"] intValue] == 23002) {
                        [SVProgressHUD showErrorWithStatus:@"评论失败"];
                    }else if ([response[@"code"] intValue] == 23003) {
                        [SVProgressHUD showErrorWithStatus:@"您还不能评论"];
                    }
                }
            }];
        }
    }

}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
